package com.sakura.mall.product.spuInfo.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.product.spuInfo.domain.entity.SpuInfoEntity;
import com.sakura.mall.product.spuInfo.domain.request.AddSpuInfoRequest;
import com.sakura.mall.product.spuInfo.domain.request.UpdateSpuInfoRequest;
import com.sakura.mall.product.spuInfo.domain.request.SpuInfoPageRequest;
import com.sakura.mall.product.spuInfo.domain.vo.SpuInfoVo;
import com.sakura.mall.product.spuInfo.mapper.SpuInfoMapper;
import com.sakura.mall.product.spuInfo.service.SpuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * spu信息 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfoEntity> implements SpuInfoService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSpuInfoRequest request) {
        // todo 其他校验逻辑
        SpuInfoEntity spuInfo = request.convert(SpuInfoEntity.class);
        this.save(spuInfo);
    }

    @Override
    public Page<SpuInfoVo> queryPage(SpuInfoPageRequest request) {
        IPage<SpuInfoEntity> qp = request.toPage();
        LambdaQueryWrapper<SpuInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<SpuInfoVo> ret = qp.convert(entity -> entity.convert(SpuInfoVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateSpuInfoRequest request) {
        SpuInfoEntity entity = request.convert(SpuInfoEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

